package com.anbinh.doctor.adapter;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ResponseDoctorPrice.Price;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.zcw.togglebutton.ToggleButton;
import com.zcw.togglebutton.ToggleButton.OnToggleChanged;

public class PricesAdapter extends ArrayAdapter<Price> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<Price> listData;
	private ImageLoader imageLoader;
	private Context mContext;

	public PricesAdapter(Context mContext, int resource, List<Price> models) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;

		layoutInflater = LayoutInflater.from(mContext);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(mContext
				.getApplicationContext()));
	}

	public class ViewHolder {
		public TextView lblName;
		public TextView lblPrice;
		public View drag_handle;
		public View viewSetup;
		public ImageView icon;
		public ToggleButton btnOnOff;

	}

	@Override
	public Price getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.lblName = (TextView) convertView
					.findViewById(R.id.lblServiceName);
			viewHolder.lblPrice = (TextView) convertView
					.findViewById(R.id.lblPrice);
			viewHolder.btnOnOff = (ToggleButton) convertView
					.findViewById(R.id.btnToggle);
			viewHolder.drag_handle = (View) convertView
					.findViewById(R.id.drag_handle);
			viewHolder.viewSetup = (View) convertView
					.findViewById(R.id.viewSetup);
			
			
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Price item = getItem(position);
		viewHolder.lblName.setText(item.getContent_price());
		viewHolder.lblPrice.setText(Utils.formatMoney(item.getPrice())+" đồng");

		if (item.isIs_active()) {
			viewHolder.btnOnOff.setToggleOn();
		} else {
			viewHolder.btnOnOff.setToggleOff();
		}
		
		if(item.isEdit()){
			viewHolder.drag_handle.setVisibility(View.VISIBLE);
			viewHolder.viewSetup.setVisibility(View.INVISIBLE);
			
		}else{
			viewHolder.drag_handle.setVisibility(View.GONE);
			viewHolder.viewSetup.setVisibility(View.VISIBLE);
		}
		
		viewHolder.btnOnOff.setOnToggleChanged(new OnToggleChanged() {

			@Override
			public void onToggle(boolean on) {
				listData.get(position).setIs_active(on);
				onOff(item.getId());
			}
		});

		return convertView;
	}

	public void showPopupMenu(final Price item) {
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				R.drawable.abs__menu_dropdown_panel_holo_light);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_menu_extaned);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);

		TextView lblXoaDc = (TextView) dialog.findViewById(R.id.lblXoaDc);
		lblXoaDc.setText("Xóa thành phần này");

		lblXoaDc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	public void onOff(int id) {
		UserLogin userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);
		ServerController.getInstance(mContext).requestOnoffComment(
				userLogin.getData().getToken(), id,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {

					}

					@Override
					public void onError(int errorCode, String message) {

					}
				});
	}
}
